package demo.controller.adm;

import demo.dao.*;
import demo.entity.Clz;
import demo.entity.Course;
import demo.entity.Task;
import demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/task")
public class TaskAdmController {
    @Autowired(required = false)
    FileDao fileDao;
    @Autowired(required = false)
    TaskDao taskDao;
    @Autowired(required = false)
    UserDao userDao;
    @Autowired(required = false)
    ClzDao clzDao;
    @Autowired(required = false)
    CourseDao courseDao;
    @RequestMapping("/show")
    public String show (Model model,HttpSession session){
    String role="tea";
    List<Task> tasks = taskDao.findByTea(role);
    List<User> tea = userDao.findByRole(role);
    List<Clz> clzs = clzDao.findAll();
    List<Course> courses = courseDao.findAll();
    model.addAttribute("lists",tasks);
    model.addAttribute("teacher",tea);
    model.addAttribute("clzs",clzs);
    model.addAttribute("courses",courses);
    return "/adm/task";
    }
    @PostMapping("/add")
    public String add (Task task,Model model){
        int add = taskDao.add(task);
        if (add==0) model.addAttribute("actions","添加失败");
        model.addAttribute("actions","添加成功");
        return "forward:/task/show";
    }
    @GetMapping("/del/{kid}")
    public String del(@PathVariable("kid") Integer kid, Model model){
        int del = taskDao.del(kid);
        if (del==0)model.addAttribute("actions","删除失败");
        model.addAttribute("actions","删除成功");
        return "forward:/task/show";
    }
}
